package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.domain.TbBrand;
import com.ruoyi.system.mapper.TbBrandMapper;
import com.ruoyi.system.service.ITbBrandService;
import com.ruoyi.system.util.PinyinUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 品牌Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-06-01
 */
@Service
public class TbBrandServiceImpl implements ITbBrandService 
{
    @Autowired
    private TbBrandMapper tbBrandMapper;

    /**
     * 查询品牌
     * 
     * @param id 品牌主键
     * @return 品牌
     */
    @Override
    public TbBrand selectTbBrandById(Integer id)
    {
        return tbBrandMapper.selectTbBrandById(id);
    }

    /**
     * 查询品牌列表
     * 
     * @param tbBrand 品牌
     * @return 品牌
     */
    @Override
    public List<TbBrand> selectTbBrandList(TbBrand tbBrand)
    {
        return tbBrandMapper.selectTbBrandList(tbBrand);
    }

    /**
     * 新增品牌
     * 
     * @param tbBrand 品牌
     * @return 结果
     */
    @Override
    public int insertTbBrand(TbBrand tbBrand)
    {
        return tbBrandMapper.insertTbBrand(tbBrand);
    }

    /**
     * 修改品牌
     * 
     * @param tbBrand 品牌
     * @return 结果
     */
    @Override
    public int updateTbBrand(TbBrand tbBrand)
    {
        return tbBrandMapper.updateTbBrand(tbBrand);
    }

    /**
     * 批量删除品牌
     * 
     * @param ids 需要删除的品牌主键
     * @return 结果
     */
    @Override
    public int deleteTbBrandByIds(Integer[] ids)
    {
        return tbBrandMapper.deleteTbBrandByIds(ids);
    }

    /**
     * 删除品牌信息
     * 
     * @param id 品牌主键
     * @return 结果
     */
    @Override
    public int deleteTbBrandById(Integer id)
    {
        return tbBrandMapper.deleteTbBrandById(id);
    }


    /***
     *  生成品牌首字母
     * @param ids
     * @return
     */
    @Override
    public int updateTbBrandList(Integer[] ids) {
        //根据id获取对应的品牌集合
        List<TbBrand> tbBrandList = tbBrandMapper.getTbBrandList(ids);
        //根据名称生成品牌首字母，批量更新对应的字段
        if(tbBrandList.size()>0){
            for(TbBrand entity:tbBrandList){
                if(StringUtils.isNotEmpty(entity.getName())){
                    //获取首字母
                    String letter = PinyinUtils.converterToFirstSpell(entity.getName());
                    entity.setLetter(letter);
                }
            }

        }
        //批量更新实体
        int i = tbBrandMapper.batchUpdate(tbBrandList);
        return i;
    }
}
